
//@ts-nocheck
// npc对话处理 {"地图":1001,"编号":106,"序列":106}

export function execute(client_id, message_json) {
    let login_characters = G.Characters.get_current_login_characters(client_id);
    // 判断玩家是否在组队中，并且不是队长
    if (login_characters["队伍"] !== 0 && _TeamDataCache.has(login_characters["队伍"]) && !login_characters["队长"]) {
        return;
    }
    //判断玩家是否在摆摊
    if (Object.keys(login_characters["摊位数据"]).length !== 0 ||
        Object.keys(login_characters["收购数据"]).length !== 0) {
        ClientUtils.send_client_dialogue(client_id, "", "系统", "摆摊中,无法对话。");
        return
    }
    let map_id = message_json["地图"];
    let characters_map_id = login_characters["地图数据"]["编号"];
    // 校验客户端当前地图位置
    if (map_id !== characters_map_id) {
        ClientUtils.send_basic_prompt(client_id, "#R非法请求,与角色所在地图不匹配");
        return;
    }

    let npc_number = message_json["编号"];
    let src_x = login_characters["地图数据"]["x"];
    let src_y = login_characters["地图数据"]["y"];

    let npc_data = undefined;

    let map_npc = G.MapNpcData.default[characters_map_id];
    if (map_npc) {
        npc_data = map_npc[npc_number];
    }

    if (npc_data === undefined) {
        // 没有配置地图固定NPC则尝试取副本NPC对话数据
        let dialogue_data = get_dungeon_npc_dialogue_data(login_characters, npc_number);
        if (dialogue_data) {
            let map_data = G.MapUtils.get_map_data(map_id);
            let npc_data = map_data.get(npc_number)
            ClientUtils.send_client_dialogue(client_id, dialogue_data[0], dialogue_data[1], dialogue_data[2], dialogue_data[3], dialogue_data[4], login_characters["剧情"], [map_id, npc_number, npc_data["名称"]]);
            ClientUtils.send_team_dialogue_data(login_characters, dialogue_data, [map_id, npc_number, npc_data["名称"]]);
            return;
        }
        // // 副本也没有 返回未配置
        // dialogue_data = ["", "系统", "该npc尚未配置对话！"];
        // ClientUtils.send_client_dialogue(client_id, dialogue_data[0], dialogue_data[1], dialogue_data[2]);
        // ClientUtils.send_team_dialogue_data(login_characters, dialogue_data, [map_id, npc_number, "系统"]);
    } else {
        // 如果要对话的npc与角色不在一个地图或距离过远则提示客户端
        if (map_id !== characters_map_id || G.MapUtils.check_point_distance(src_x, src_y, npc_data["x"], npc_data["y"], 170)) {
            ClientUtils.send_basic_prompt(client_id, "#Y/这个npc距离太远了");
            return;
        }
        // 获取地图固定NPC剧情对话数据
        if (login_characters["剧情"] && map_id === login_characters["剧情"]["地图"] && npc_number === login_characters["剧情"]["编号"] && (login_characters["队伍"] === 0 || login_characters["队长"])) {
            let plot_level = [0, 0, 25, 55, 75, 80, 85, 135, 155];
            let plot_level_index = login_characters["剧情"]["主线"] - 1;
            // 校验主线剧情等级
            if (login_characters["等级"] >= plot_level[plot_level_index]) {
                //取剧情对话
                let plot_dialogue_func = _DialogueData.get("plot_dialogue")[login_characters["剧情"]["主线"]][login_characters["剧情"]["进度"]];
                if (plot_dialogue_func !== undefined) {
                    let plot_dialogue = plot_dialogue_func(login_characters, npc_data);
                    if (plot_dialogue) {
                        ClientUtils.send_client_dialogue(client_id, plot_dialogue[0], plot_dialogue[1], plot_dialogue[2], plot_dialogue[3], plot_dialogue[4], login_characters["剧情"], [map_id, npc_number, npc_data["名称"]]);
                        ClientUtils.send_team_dialogue_data(login_characters, plot_dialogue, [map_id, npc_number, npc_data["名称"]]);
                        return;
                    }
                }
            }
        }
        // 获取地图固定NPC对话数据
        let dialogue_data = get_map_npc_dialogue_data(login_characters["id"], map_id, npc_number);
        if (dialogue_data) {
            _LastDialogueData.set(login_characters["id"], { "模型": npc_data["模型"], "名称": npc_data["名称"] });
            ClientUtils.send_client_dialogue(client_id, dialogue_data[0], dialogue_data[1], dialogue_data[2], dialogue_data[3], dialogue_data[4], login_characters["剧情"], [map_id, npc_number, npc_data["名称"]]);
            ClientUtils.send_team_dialogue_data(login_characters, dialogue_data, [map_id, npc_number, npc_data["名称"]]);
        } else {
            console.warn(`未在地图[${map_id}]中定义NPC[${npc_data["名称"]}],编号[${npc_number}] 的对话数据`);
        }
    }
}


function get_dungeon_npc_dialogue_data(characters, npc_number) {
    if ((typeof npc_number) === "string") {
        let splits = npc_number.split("_");
        if (splits.length === 7) {
            let npc_map_id = splits[0];
            let dungeon_id = splits[1];
            let npc_characters_id = splits[2];
            let map_id = characters["地图数据"]["编号"];
            let src_x = characters["地图数据"]["x"];
            let src_y = characters["地图数据"]["y"];
            let map_data = G.MapUtils.get_map_data(Number(npc_map_id));
            let npc_data = map_data.get(npc_number)
            if (npc_data === undefined) {
                ClientUtils.send_characters_basic_prompt(characters["id"], "#R非法数据,当前地图不存在该NPC");
                return;
            }
            // 校验npc编号合法性
            if (Number(npc_characters_id) === characters["id"]) {
                if (Number(npc_map_id) !== map_id || G.MapUtils.check_point_distance(src_x, src_y, npc_data["x"], npc_data["y"], 170)) {
                    ClientUtils.send_characters_basic_prompt(characters["id"], "#Y/这个npc距离太远了");
                    return;
                }
                //获取对话
                let dungeon_npc_dialogue = _DialogueData.get(`${dungeon_id}_${map_id}`);
                if (dungeon_npc_dialogue && dungeon_npc_dialogue[npc_data["名称"]]) {
                    let plot_dialogue_fn = dungeon_npc_dialogue[npc_data["名称"]];
                    return plot_dialogue_fn(characters, npc_data);
                }
            } else {
                ClientUtils.send_characters_basic_prompt(characters["id"], "#R数据非法!")
                return;
            }
        }
    }
}

/**
 * 获取常规地图npc对话内容
 * @param {*} characters_id 
 * @param {*} map_id 
 * @param {*} npc_number 
 * @returns 
 */
function get_map_npc_dialogue_data(characters_id, map_id, npc_number) {
    let dialogue_data = _DialogueData.get(`${map_id}`);
    if (!dialogue_data) {
        return;
    } else {
        let npc_dialogue_data = dialogue_data[npc_number];
        if (npc_dialogue_data === undefined) {
            return;
        } else {
            return typeof npc_dialogue_data === "function" ? npc_dialogue_data(characters_id) : npc_dialogue_data;
        }
    }
}
